/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package layouteditor.view;

import folder.helper.MenuLayoutElement;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import layoutitems.CurrentLayoutElements;
import layoutitems.ImageElement;
import modelviewcontroller.MenuBarController;

public class MainMenuBar implements ActionListener {

    private static JMenuBar menuBar;
    //Menüs
    private JMenu menuFile;
    private JMenu menuEdit;
    private JMenu menuHelp;
    // UnterMenüs
    private JMenu menuFileNew;
    private JMenuItem menuItemFileNewText;
    private JMenuItem menuItemFileNewImage;
    private JMenuItem menuItemFileOpen;
    private JMenuItem menuItemFileSave;
    private JMenuItem menuItemFileSaveAs;
    private JMenuItem menuItemFileImportLayoutElement;
    private JMenuItem menuItemFileExit;
    private JMenuItem menuItemEditCut;
    private JMenuItem menuItemEditCopy;
    private JMenuItem menuItemEditPaste;
    private JMenuItem menuItemHelpHelp;
    //FileChooser
    private JFrame jFrame;
    //ModelViewController
    private MenuBarController modelViewController;
    //Verknüpfung zw. MenuItem und LayoutElement
    private static ArrayList<MenuLayoutElement> submenuArray;

    public MainMenuBar(JFrame setFrame) throws IOException {

        //Controller initialisieren
        modelViewController = new MenuBarController();
        //Erstellen einer Menüleiste
        menuBar = new JMenuBar();
        jFrame = setFrame;
        //Hinzufügen von Menüs
        menuFile = new JMenu("Datei");
        menuEdit = new JMenu("Bearbeiten");
        menuHelp = new JMenu("Hilfe");

        menuBar.add(menuFile);
        menuBar.add(menuEdit);
        menuBar.add(menuHelp);
        //Hinzufügen von Untermenüs
        menuFileNew = new JMenu("Neu");
        menuFile.add(menuFileNew);

        //Hinzufügen von Menüeinträgen in das Dateimenü
        menuItemFileNewText = new JMenuItem("Text");
        menuItemFileNewText.addActionListener(this);
        menuItemFileNewImage = new JMenuItem("Bild");
        menuItemFileNewImage.addActionListener(this);
        menuItemFileOpen = new JMenuItem("Öffnen");
        menuItemFileOpen.addActionListener(this);
        menuItemFileSave = new JMenuItem("Speichern");
        menuItemFileSave.addActionListener(this);
        menuItemFileSaveAs = new JMenuItem("Speichern als");
        menuItemFileSaveAs.addActionListener(this);
        menuItemFileImportLayoutElement = new JMenuItem("Layout-Element importieren");
        menuItemFileImportLayoutElement.addActionListener(this);
        menuItemFileExit = new JMenuItem("Beenden");
        menuItemFileExit.addActionListener(this);

        menuFileNew.add(menuItemFileNewText);
        menuFileNew.add(menuItemFileNewImage);
        menuFile.add(menuItemFileOpen);
        menuFile.add(menuItemFileSave);
        menuFile.add(menuItemFileSaveAs);
        menuFile.addSeparator();
        menuFile.add(menuItemFileImportLayoutElement);
        menuFile.addSeparator();
        menuFile.add(menuItemFileExit);
        /*
         * Hinzufügen von Menüeinträgen in das Bearbeitenmenü
         */
        menuItemEditCut = new JMenuItem("Ausschneiden");
        menuItemEditCut.addActionListener(this);
        menuItemEditCopy = new JMenuItem("Kopieren");
        menuItemEditCopy.addActionListener(this);
        menuItemEditPaste = new JMenu("Einfügen");
        menuItemEditPaste.addActionListener(this);
        //Layout Elemente zum einfügen
        submenuArray = InsertOnRightClickMenu.getMenuLayoutElementConnectionArray(this);
        for (MenuLayoutElement mle : submenuArray) {
            menuItemEditPaste.add(mle.getMenuItem());
        }

        menuEdit.add(menuItemEditCut);
        menuEdit.add(menuItemEditCopy);
        menuEdit.add(menuItemEditPaste);

        //Hinzufügen von Menüeinträgen in das Hilfemenü
        menuItemHelpHelp =
                new JMenuItem("Hilfe");

        menuHelp.add(menuItemHelpHelp);
    }

    public JMenuBar getMenuBar() {
        return menuBar;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == menuItemFileNewText) {
            System.out.println("neuText");
        }
        if (e.getSource() == menuItemFileNewImage) {
            System.out.println("neuBild");
        }
        if (e.getSource() == menuItemFileOpen) {
            MenuBarController.openLayoutFile(jFrame);
        }
        if (e.getSource() == menuItemFileSave) {
            MenuBarController.saveCurrentLayout(MenuBarController.SAVE, null);
        }
        if (e.getSource() == menuItemFileSaveAs) {
            MenuBarController.saveCurrentLayout(MenuBarController.SAVE_AS, jFrame);
        }
        if (e.getSource() == menuItemFileImportLayoutElement) {
            MenuBarController.importNewLayoutElement(jFrame);
        }
        if (e.getSource() == menuItemFileExit) {
            System.out.println("beenden");
            System.exit(0);
        }
        if (e.getSource() == menuItemEditCut) {
            System.out.println("ausschneiden");
        }
        if (e.getSource() == menuItemEditCopy) {
            System.out.println("kopieren");
        }
        if (e.getSource() == menuItemEditPaste) {
           //Einfügen neuer LayoutElemente
            MenuBarController.insertNewLayoutElementIntoView(submenuArray, jFrame, e);
        }
        if (e.getSource() == menuItemHelpHelp) {
            System.out.println("hilfe");
        }
    }
}